package com.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.XiaofeiDAO;
import com.pojo.Dictionary;
import com.pojo.Xiaofei;
import com.util.ValidateHelper;
import com.vo.XiaofeiVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("xfService")
public class XiaofeiService extends ServiceImpl<XiaofeiDAO, Xiaofei> {

    @Autowired
    private XiaofeiDAO xfDao;

    @Autowired
    private DictionaryService dictionaryService;

    public IPage<XiaofeiVo> getCzRecordByUser(int page, int limit , String staffId, String time1, String time2){
        IPage<XiaofeiVo> xfList = xfDao.getXfRecordByUser(new Page(page,limit),staffId,time1, time2);
        return xfList;
    }

    public int saveXf(Xiaofei xf){
        return xfDao.insert(xf);
    }

    public IPage<XiaofeiVo> getXfRecord(int page, int limit, String staffName, String time1, String time2, String queryStr) {
        List<String> zyNameList = new ArrayList<String>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<Dictionary>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
        }
        return xfDao.getXfRecord(new Page(page,limit), staffName, time1, time2, queryStr, zyNameList);
    }

    public List<XiaofeiVo> exportByExecl(String staffName, String time1, String time2, String queryStr) {
        List<String> zyNameList = new ArrayList<String>();
        if (ValidateHelper.isNotEmptyString(queryStr)) {
            List<Dictionary> zyList = new ArrayList<Dictionary>();
            if ("gs".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyGs();
            }else if ("jt".equals(queryStr)) {
                zyList = dictionaryService.getDicByZyJt();
            }
            if (ValidateHelper.isNotEmptyCollection(zyList)) {
                for (Dictionary dictionary : zyList) {
                    if (ValidateHelper.isNotEmptyString(dictionary.getName())) {
                        zyNameList.add(dictionary.getName());
                    }
                }
            }
        }
        return xfDao.exportByExecl(staffName, time1, time2, queryStr, zyNameList);
    }

    public List<Map<String,Object>> getDayAmountByMonth(){
        return xfDao.getDayAmountByMonth();
    }

    public BigDecimal getTotalAmount(){
        return xfDao.getTotalAmount();
    }
}
